package com.example.demo.breakrulemanage.service.impl;

import com.example.demo.breakrulemanage.mapper.BreakRuleMapper;
import com.example.demo.breakrulemanage.service.BreakRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class BreakRuleServiceImpl implements BreakRuleService {
    @Autowired
    private BreakRuleMapper breakRuleMapper;


    /**
         * @Description 新增加油记录
         * @Author zhangxue
         * @Date 11:18 2018/7/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveBreakRule(Map<String, Object> param) {
        int i = breakRuleMapper.saveBreakRuleMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            int j = breakRuleMapper.updateSjBrFlagMapper(param);
            if(j<0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("status","-1");
            }else{
                resultMap.put("status","1");
            }
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 处理违章记录
     * @Author zhangxue
     * @Date 11:18 2018/7/12
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editBreakRule(Map<String, Object> param) {
        int i = breakRuleMapper.editBreakRuleMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            //处理完当前违章记录后查询该司机是否还有违章记录
            List<Map<String,Object>> BreakRuleList = breakRuleMapper.queryAllBreakRuleBySjNo(param);
            if(BreakRuleList.size()>0){
                //当前司机还有违章记录，所以不修改司机表违章状态
                resultMap.put("status","2");
            }else{
                //当前司机无违章记录，修改司机表违章状态
                int j = breakRuleMapper.updateSjBrFlag(param);
                if(j>0){
                    resultMap.put("status","1");
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultMap.put("status","0");
                }
            }
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 删除违章记录
     * @Author zhangxue
     * @Date 11:18 2018/7/12
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delBreakRule(Map<String, Object> param) {
        int i = breakRuleMapper.delBreakRuleMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            //删除完当前违章记录后查询该司机是否还有违章记录
            List<Map<String,Object>> BreakRuleList = breakRuleMapper.queryAllBreakRuleBySjNo(param);
            if(BreakRuleList.size()>0){
                //当前司机还有违章记录，所以不修改司机表违章状态
                resultMap.put("status","2");
            }else{
                //当前司机无违章记录，修改司机表违章状态
                int j = breakRuleMapper.updateSjBrFlag(param);
                if(j>0){
                    resultMap.put("status","1");
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultMap.put("status","0");
                }
            }
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 查询所有加油记录
         * @Author zhangxue
         * @Date 11:00 2018/11/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryAllBreakRule(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = breakRuleMapper.queryAllBreakRule(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        return resultMap;

    }



}
